package main

import "fmt"

type node struct {
	next *node
	value interface{}
}

type oneWayLinkedList struct {
	head *node
}

func newOneWayLinkedList() *oneWayLinkedList {
	list := new(oneWayLinkedList)
	list.head = new(node)

	return list
}

func (list *oneWayLinkedList) add(n *node) {
	currNode := list.head
	for currNode.next != nil {
		currNode = currNode.next
	}
	currNode.next = n
}

func (list *oneWayLinkedList) show() {
	currNode := list.head.next
	for currNode != nil {
		fmt.Print(currNode.value, " ")
		currNode = currNode.next
	}
	fmt.Println()
}

func (list *oneWayLinkedList) reverse() {

	currNode := list.head.next
	var prevNode *node

	for currNode != nil {
		tempNode := currNode.next
		currNode.next = prevNode
		prevNode = currNode
		currNode = tempNode
	}

	list.head.next = prevNode
}

func main() {
	list := newOneWayLinkedList()
	for i := 0; i < 5; i++ {
		n := new(node)
		n.value = i
		list.add(n)
	}

	list.show()
	list.reverse()
	list.show()
}

